package com.gxuwz.weather.utils;

import android.content.Context;
import android.util.Log;

import com.gxuwz.weather.api.VersionApi;
import com.gxuwz.weather.model.VersionInfo;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UpdateChecker {
    private static final String TAG = "UpdateChecker";

    // Gitee Raw文件地址
    private static final String GITEE_RAW_URL = "https://gitee.com/w-rj/weather/releases/download/weatherv3.0/";
    
    // 选择使用哪个平台
    private static final String BASE_URL = GITEE_RAW_URL; // 或者 GITHUB_RAW_URL
    
    private Context context;
    private OnUpdateListener listener;
    
    public interface OnUpdateListener {
        void onUpdateAvailable(VersionInfo versionInfo);
        void onNoUpdate();
        void onError(String error);
    }
    
    public UpdateChecker(Context context) {
        this.context = context;
    }
    
    public void setOnUpdateListener(OnUpdateListener listener) {
        this.listener = listener;
    }
    
    /**
     * 检查更新
     */
    public void checkUpdate() {
        try {
            // 创建Retrofit实例
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            
            // 创建API接口
            VersionApi versionApi = retrofit.create(VersionApi.class);
            
            // 发起网络请求
            Call<VersionInfo> call = versionApi.getLatestVersion();
            call.enqueue(new Callback<VersionInfo>() {
                @Override
                public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        VersionInfo serverVersion = response.body();
                        int currentVersionCode = VersionUtils.getCurrentVersionCode(context);
                        
                        Log.d(TAG, "当前版本: " + currentVersionCode + ", 服务器版本: " + serverVersion.getVersionCode());
                        
                        if (serverVersion.getVersionCode() > currentVersionCode) {
                            // 有新版本
                            if (listener != null) {
                                listener.onUpdateAvailable(serverVersion);
                            }
                        } else {
                            // 已是最新版本
                            if (listener != null) {
                                listener.onNoUpdate();
                            }
                        }
                    } else {
                        String errorMsg = "服务器响应异常";
                        if (response.code() == 404) {
                            errorMsg = "版本信息文件不存在，请稍后再试！";
                        } else if (response.code() >= 500) {
                            errorMsg = "服务器内部错误";
                        } else {
                            errorMsg = "服务器响应异常: " + response.code();
                        }
                        
                        if (listener != null) {
                            listener.onError(errorMsg);
                        }
                    }
                }
                
                @Override
                public void onFailure(Call<VersionInfo> call, Throwable t) {
                    Log.e(TAG, "检查更新失败", t);
                    String errorMsg = "网络请求失败";
                    if (t instanceof java.net.UnknownHostException) {
                        errorMsg = "无法连接到Git服务器，请检查网络连接";
                    } else if (t instanceof java.net.SocketTimeoutException) {
                        errorMsg = "网络请求超时，请稍后重试";
                    } else {
                        errorMsg = "网络请求失败: " + t.getMessage();
                    }
                    
                    if (listener != null) {
                        listener.onError(errorMsg);
                    }
                }
            });
        } catch (Exception e) {
            Log.e(TAG, "创建网络请求失败", e);
            if (listener != null) {
                listener.onError("创建网络请求失败: " + e.getMessage());
            }
        }
    }
} 